package com.iflyun.redis.dao.impl;



import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;


import com.iflyun.redis.dao.RedisHashDao;
import com.iflyun.redis.entity.Bus;
import com.iflyun.redis.entity.Redis;
import com.iflyun.redis.test.RedisClient;

public class RedisHashDaoImpl implements RedisHashDao {
	
	@Resource(name = "redisTemplate")
	protected RedisTemplate<String, Redis> redisTemplate;

	@Override
	public boolean add(final Redis redis) {
		
		return redisTemplate.execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
         	  
            	
            	boolean result=connection.hSetNX(
                		redisTemplate.getStringSerializer().serialize(redis.getKey()), 
                		 redisTemplate.getStringSerializer().serialize("key"),
                		 redisTemplate.getStringSerializer().serialize(redis.getKey())
                		 );
            	
            	
                boolean result1=connection.hSetNX(
                		redisTemplate.getStringSerializer().serialize(redis.getKey()), 
                		 redisTemplate.getStringSerializer().serialize("value"),
                		 redisTemplate.getStringSerializer().serialize(redis.getValue())
                		 );
                return result&&result1;
            }
	    },false,true);
		
	}

	@Override
	public Redis get(final String keyId) {
		return redisTemplate.execute(new RedisCallback<Redis>() {
            @Override
            public Redis doInRedis(RedisConnection connection) throws DataAccessException {
           	 byte[] key=connection.hGet(redisTemplate.getStringSerializer().serialize(keyId), 
                		redisTemplate.getStringSerializer().serialize("key"));
        	 byte[] value=connection.hGet(redisTemplate.getStringSerializer().serialize(keyId), 
             		redisTemplate.getStringSerializer().serialize("value"));
        	 String k = redisTemplate.getStringSerializer().deserialize(key);
        	 String v = redisTemplate.getStringSerializer().deserialize(value);
        	 return new Redis(k, v);
            }
	    });
	}

	@Override
	public List<String> getLike(String match,int count) {
		/*final List<String> list=new ArrayList<String>();
		
		List<String> result=redisTemplate.execute(new RedisCallback<List<String>>() {  
            public List<String> doInRedis(RedisConnection connection)  
                    throws DataAccessException { 
            	 ScanOptionsBuilder sob=new  ScanOptionsBuilder();
                 sob.count(count);
                 sob.match(match);
                 ScanOptions options= sob.build();
                 Cursor<byte[]> keys=connection.scan(options);  
                 int i=0;
                 while (keys.hasNext()) {
                	System.out.println(keys.getCursorId()+":"+keys.getPosition());
                	String key = redisTemplate.getStringSerializer().deserialize(keys.next());  
                	list.add(key);               	 
                	i++;
				}
                System.out.println(i);        
                return list;  
            }  
        });
		return result;*/
		
		RedisClient rc=new RedisClient();
		return rc.ScanBus(match, count);
	
		
	}

	@Override
	public boolean addBus(final Bus bus ,final String type,final long ttl) {
		return redisTemplate.execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
            	String key=bus.getNo()+":"+type+":"+bus.getRealName()+":"+bus.getId();
            	boolean result=connection.hSetNX(
                		redisTemplate.getStringSerializer().serialize(key), 
                		 redisTemplate.getStringSerializer().serialize("Number"),
                		 redisTemplate.getStringSerializer().serialize(bus.getNo())
                		 );
            	
            	
                boolean result1=connection.hSetNX(
                	 redisTemplate.getStringSerializer().serialize(key), 
               		 redisTemplate.getStringSerializer().serialize("Name"),
               		 redisTemplate.getStringSerializer().serialize(bus.getName())
               		 );
                
                boolean result2=connection.hSetNX(
                   	 redisTemplate.getStringSerializer().serialize(key), 
                  		 redisTemplate.getStringSerializer().serialize("RealName"),
                  		 redisTemplate.getStringSerializer().serialize(bus.getRealName())
                  		 );
                
                boolean result3=connection.hSetNX(
                      	 redisTemplate.getStringSerializer().serialize(key), 
                     		 redisTemplate.getStringSerializer().serialize("Type"),
                     		 redisTemplate.getStringSerializer().serialize(type)
                     		 );
                
                
                connection.expire(redisTemplate.getStringSerializer().serialize(key), ttl);
                return result&&result1&&result2&&result3;
            }
	    });
	}

	@Override
	public Bus getBus(final String keyId) {
		return redisTemplate.execute(new RedisCallback<Bus>() {
            @Override
            public Bus doInRedis(RedisConnection connection) throws DataAccessException {
           	 byte[] number=connection.hGet(redisTemplate.getStringSerializer().serialize(keyId), 
                		redisTemplate.getStringSerializer().serialize("Number"));
        	 byte[] name=connection.hGet(redisTemplate.getStringSerializer().serialize(keyId), 
             		redisTemplate.getStringSerializer().serialize("Name"));      	 
        	 byte[] realName=connection.hGet(redisTemplate.getStringSerializer().serialize(keyId), 
              		redisTemplate.getStringSerializer().serialize("RealName"));
        	 String snumber = redisTemplate.getStringSerializer().deserialize(number);
        	 String sname = redisTemplate.getStringSerializer().deserialize(name);
        	 String srealName =redisTemplate.getStringSerializer().deserialize(realName);
        	 return new Bus(snumber, sname, srealName);
            }
	    });
	}

}
